#! /usr/bin/python2.4

# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""check_gvn_revprops.py unittest"""


import unittest

import svn.repos

import gvn.hooks.info
import testing.hooks.common

# import RunHook to pass the function pointer to testcommon in unittests
from gvn.hooks.pre_commit.check_gvn_revprops import RunHook


class TestCase(testing.hooks.common.HookTestCase):

  def testGvnApprove(self):
    """gvn:approve* blocked as part of commit
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:approve", "",
                          "polly", "REV"),
                          "gvn:approve* not allowed at commit time")
    self.assertHookResult(self._TestProp(RunHook, "gvn:approved", "",
                          "polly", "REV"),
                          "gvn:approve* not allowed at commit time")
    self.assertHookResult(self._TestProp(RunHook, "gvn:approve:polly", "",
                          "polly", "REV"),
                          "gvn:approve* not allowed at commit time")

  def testGvnSubmitted(self):
    """gvn:submitted blocked as part of commit
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:submitted", "",
                          "polly", "REV"),
                          "gvn:submitted not allowed at commit time")

  def testGvnChangeInvalid(self):
    """Improperly formatted gvn:change blocked
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "",
                          "polly", "REV"), "Invalid gvn:change value ")
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "polly/x8c6",
                          "polly", "REV"),
                          "Invalid gvn:change value polly/x8c6")
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "polly/x8c6@",
                          "polly", "REV"),
                          "Invalid gvn:change value polly/x8c6@")
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "polly/x8c6@q",
                          "polly", "REV"),
                          "Invalid gvn:change value polly/x8c6@q")
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "manuel/x8c@1",
                          "polly", "REV"),
                          "polly not allowed to submit manuel's change"
                          " (manuel/x8c@1)")
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "polly/x8c@-1",
                          "polly", "REV"),
                          "Invalid revision number for gvn:change -1"
                          " (polly/x8c@-1)")
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "polly/x8c6@0",
                          "polly", "REV"),
                          "Invalid revision number for gvn:change 0"
                          " (polly/x8c6@0)")
    # Surely r1000 won't exist in our test repo...
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", 
                          "polly/x8c6@1000", "polly", "REV"),
                          "Invalid revision number for gvn:change 1000"
                          " (polly/x8c6@1000)")

  def testGvnChangeValid(self):
    """Poperly formatted gvn:change allowed
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:change", "polly/x8c6@1",
                          "polly", "REV"), 0)
